﻿using Mapster;
using Microsoft.Extensions.Logging;
using NPOI.HPSF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMV.Application.Tr;
using TMV.Core.CM;
using TMV.DTO;
using TMV.DTO.SocketConfig;

namespace TMV.Application.SocketConfig.Service
{
    public class SocketConfigService: ISocketConfigService,ITransient
    {
        ISqlSugarClient _db;
        private readonly ILogger<SocketConfigService> _logger;
        public SocketConfigService(ISqlSugarClient db, ILogger<SocketConfigService> logger)
        {
            _logger = logger;
            _db = db;
        }
        public ResultEntity<SocketConfigDto> GetSocketConfig(string ip,string code)
        {
            var model = _db.Queryable<TMV_SocketConfig>()
                .WhereIF(!string.IsNullOrWhiteSpace(ip),x => x.Ip == ip)
				.WhereIF(!string.IsNullOrWhiteSpace(code), x => x.Code == code)
				.First();
            if(model == null) return new ResultEntity<SocketConfigDto>() { Data = null, IsSuccess = false };
			var result = model.Adapt<SocketConfigDto>();
            return new ResultEntity<SocketConfigDto>() { Data = result, IsSuccess = true };
        }
		public ResultEntity<List<SocketConfigDto>> GetSocketConfig()
		{
			var model = _db.Queryable<TMV_SocketConfig>().ToList();
			var result = model.Adapt<List<SocketConfigDto>>();
			return new ResultEntity<List<SocketConfigDto>>() { Data = result, IsSuccess = true };
		}
        public bool UpdateStatus(string ip,bool status)
        {
			var model = _db.Queryable<TMV_SocketConfig>().Where(x => x.Ip == ip).First();
			model.Status=status;
            return _db.Updateable(model).ExecuteCommand()>0;
		}
        public ResultEntity<bool> AddSocketConfig(SocketConfigModel socketConfigModel)
        {
           
            if (string.IsNullOrWhiteSpace(socketConfigModel.Code)) return new ResultEntity<bool>() { IsSuccess = false, Msg = "业务编码不能为空" };
            if (string.IsNullOrWhiteSpace(socketConfigModel.Ip)) return new ResultEntity<bool>() { IsSuccess= false, Msg="Ip地址不能为空"};
            if (string.IsNullOrWhiteSpace(socketConfigModel.Port)) return new ResultEntity<bool>() { IsSuccess = false, Msg = "端口号不能为空" };
            var model = socketConfigModel.Adapt<TMV_SocketConfig>();
            model.Id= Guid.NewGuid();
            model.Status = false;
            var result=_db.Insertable(model).ExecuteCommand();
            if(result<=0) return new ResultEntity<bool>() { IsSuccess = false, Msg = "添加失败" };
            return new ResultEntity<bool>() {IsSuccess = true, Msg = "添加成功" };
        }
        public ResultEntity<bool>UpSocketConfig(SocketConfigModel socketConfigModel)
        {

            if (string.IsNullOrWhiteSpace(socketConfigModel.Code)) return new ResultEntity<bool>() { IsSuccess = false, Msg = "业务编码不能为空" };
            if (string.IsNullOrWhiteSpace(socketConfigModel.Ip)) return new ResultEntity<bool>() { IsSuccess = false, Msg = "Ip地址不能为空" };
            if (string.IsNullOrWhiteSpace(socketConfigModel.Port)) return new ResultEntity<bool>() { IsSuccess = false, Msg = "端口号不能为空" };
            var model = socketConfigModel.Adapt<TMV_SocketConfig>();
            var result = _db.Updateable<TMV_SocketConfig>(model).ExecuteCommand();
            if (result <= 0) return new ResultEntity<bool>() { IsSuccess = false, Msg = "修改失败" };
            return new ResultEntity<bool>() { IsSuccess = true, Msg = "修改成功" };
        }
        public ResultEntity<bool> DelSocketConfig(string id)
        {
            var model= _db.Queryable<TMV_SocketConfig>().Where(x=>x.Id.ToString()==id).First();
            if (model == null) return new ResultEntity<bool>() { IsSuccess = false, Msg = "数据不存在" };
            var result = _db.Deleteable<TMV_SocketConfig>(model).ExecuteCommand();
            if (result <= 0) return new ResultEntity<bool>() { IsSuccess = false, Msg = "删除失败" };
            return new ResultEntity<bool>() { IsSuccess = true, Msg = "删除成功" };
        }
    }
}
